package com.springcloud.travel.scenery.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spingcloud.travel.common.TableResult;
import com.springcloud.travel.entity.Scenery;
import com.springcloud.travel.scenery.service.SceneryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("sys/scenery")
public class SceneryController {
    @Autowired
    private SceneryService sceneryService;

    @RequestMapping("/table")
    public TableResult getTable(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "5") Integer limit) {
        System.out.println("SceneryController.getTable");
        Page<Scenery> pages = new Page<>(page,limit);

        Page<Scenery> sceneryPage = sceneryService.page(pages);
        return TableResult.getSuccess((int) sceneryPage.getTotal(), sceneryPage.getRecords());
    }

    @RequestMapping("/add")
    public Boolean addMeals(@RequestBody Scenery scenery) {
        try {
            boolean insert = sceneryService.save(scenery);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequestMapping("/update")
    public Boolean updateMeals(@RequestBody Scenery scenery) {
        try {
            boolean b = sceneryService.updateById(scenery);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    @RequestMapping("/del")
    public Boolean delMeals(@RequestParam("ids") String[] ids) {
        try {
            sceneryService.removeByIds(Arrays.asList(ids));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
